<?php

namespace app\components;

use Yii;
use yii\base\ActionFilter;
use yii\web\GoneHttpException;

class PtAccessControl extends ActionFilter
{
    public $rules = [];

    public function init()
    {
        parent::init();
    }

    public function beforeAction($action)
    {
        $session = Yii::$app->session;

        if ($session->get('pt_id') == 0) {
            foreach ($this->rules as $rule) {
                if ($rule['actions'] == $action->id && $rule['allow'])
                    return true;
            }
            $this->denyAccess();
            return false;
        }

        return true;
    }

    protected function denyAccess()
    {
        throw new GoneHttpException('请先选择患者.');
        // return Yii::$app->getResponse()->redirect('/search/index');
    }
}

?>